//
//  CoverView.m
//  Shengzhuli
//
//  Created by FXGuo on 17/3/20.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "CoverView.h"

#define KAnimationTimer 0.3
@interface CoverView()

@property (nonatomic,weak) UIImageView *backView;
@property (nonatomic,strong) NSArray *titles;

@end

@implementation CoverView

- (instancetype)initWithFrame:(CGRect)frame Titles:(NSArray *)titles backStart:(CGFloat)start
{
    if (self = [super initWithFrame:frame]) {
        self.titles = titles;
        UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenW-120-8, start, 120, 40+titles.count*30)];
        backView.image = GetImage(@"popover_background_right");
        backView.userInteractionEnabled = YES;
        [self addSubview:backView];
        backView.clipsToBounds = YES;
        self.backView = backView;
        
        self.backView.layer.anchorPoint = CGPointMake(1, 0);
        self.backView.layer.position = CGPointMake(ScreenW-8, start);
        self.backView.transform = CGAffineTransformMakeScale(0, 0);
        self.backView.alpha = 0.0;
        for (NSInteger i=0; i<titles.count; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10, 25+30*i, 100, 30)];
            [btn setTitle:self.titles[i] forState:UIControlStateNormal];
            btn.titleLabel.font = Font(16);
            btn.tag = i;
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:btn];
            if (btn.tag != titles.count-1) {
                UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 25+30*(i+1), 100, 0.5)];
                lineView.backgroundColor = [UIColor whiteColor];
                [backView addSubview:lineView];
            }
        }
    }
    return self;
}
- (void)showSelectView
{
    self.backgroundColor = [UIColor clearColor];
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:KAnimationTimer animations:^{
        self.backView.transform = CGAffineTransformIdentity;
        self.backView.alpha = 1.0;
    }];
}
- (void)btnClick:(UIButton *)btn
{
    [self dismiss];
    __WEAK
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KAnimationTimer * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (weakSelf.btnBlock) {
            weakSelf.btnBlock(btn.tag);
        }
    });
}
- (void)dismiss
{
    [UIView animateWithDuration:KAnimationTimer animations:^{
        self.backView.transform = CGAffineTransformScale(self.backView.transform, 0.01, 0.01);
        self.backView.alpha = 0.0;
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(KAnimationTimer * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self removeFromSuperview];
    });
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}

//没用到
- (CABasicAnimation *)getAnimationWithValue:(NSNumber *)toValue duration:(double)dura
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = dura;
    animation.repeatCount = 1;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];
    animation.toValue = toValue;
    return animation;
}
@end
